###############################################
# Meta-analysis 9: Attitudes Toward Wartime Enemies
###############################################

# 0) User paths  edit these
PATH <- ""  

path_meta_enemies        <- file.path(PATH, "Datasets", "meta.warenemies.csv")
path_meta_enemies_biv    <- file.path(PATH, "Datasets", "meta.warenemies_biv.csv")
path_meta_enemies_random <- file.path(PATH, "Datasets", "meta.warenemies_random.csv")
path_out_csv             <- file.path(PATH, "Results",  "9.results_warenemies_models.csv")

# 1) Settings
outcome_name <- "Attitudes toward wartime enemies"

# 2) Packages
suppressPackageStartupMessages({
  library(metafor)
})

# 3) Load data
meta.enemies        <- read.csv(path_meta_enemies,        stringsAsFactors = FALSE)
meta.enemies_biv    <- read.csv(path_meta_enemies_biv,    stringsAsFactors = FALSE)
meta.enemies_random <- read.csv(path_meta_enemies_random, stringsAsFactors = FALSE)

# 4) Basic checks
required_cols <- c("authoryear", "yi", "vi")
if (!all(required_cols %in% names(meta.enemies))) {
  stop("meta.enemies is missing required columns: ",
       paste(setdiff(required_cols, names(meta.enemies)), collapse = ", "))
}
if (!all(required_cols %in% names(meta.enemies_biv))) {
  stop("meta.enemies_biv is missing required columns: ",
       paste(setdiff(required_cols, names(meta.enemies_biv)), collapse = ", "))
}
if (!all(required_cols %in% names(meta.enemies_random))) {
  stop("meta.enemies_random is missing required columns: ",
       paste(setdiff(required_cols, names(meta.enemies_random)), collapse = ", "))
}

# 5) Full, all  three level RE
full_all_fit <- rma.mv(
  yi, vi,
  random = ~ 1 | authoryear/row.names(meta.enemies),
  data   = meta.enemies,
  test   = "t"
)
full_all_df <- data.frame(
  coef        = unname(full_all_fit$beta),
  ci.lb95     = full_all_fit$ci.lb,
  ci.ub95     = full_all_fit$ci.ub,
  pval        = full_all_fit$pval,
  outcome     = outcome_name,
  n           = length(unique(meta.enemies$authoryear)),
  k           = nrow(meta.enemies),
  sample_type = "All",
  model_type  = "Full",
  stringsAsFactors = FALSE
)

# 6) Bivariate, all  three level RE
biv_all_fit <- rma.mv(
  yi, vi,
  random = ~ 1 | authoryear/row.names(meta.enemies_biv),
  data   = meta.enemies_biv,
  test   = "t"
)
biv_all_df <- data.frame(
  coef        = unname(biv_all_fit$beta),
  ci.lb95     = biv_all_fit$ci.lb,
  ci.ub95     = biv_all_fit$ci.ub,
  pval        = biv_all_fit$pval,
  outcome     = outcome_name,
  n           = length(unique(meta.enemies_biv$authoryear)),
  k           = nrow(meta.enemies_biv),
  sample_type = "All",
  model_type  = "Bivariate",
  stringsAsFactors = FALSE
)

# 7) Full, quasi-experimental  three level RE
full_random_fit <- rma.mv(
  yi, vi,
  random = ~ 1 | authoryear/row.names(meta.enemies_random),
  data   = meta.enemies_random,
  test   = "t"
)
full_random_df <- data.frame(
  coef        = unname(full_random_fit$beta),
  ci.lb95     = full_random_fit$ci.lb,
  ci.ub95     = full_random_fit$ci.ub,
  pval        = full_random_fit$pval,
  outcome     = outcome_name,
  n           = length(unique(meta.enemies_random$authoryear)),
  k           = nrow(meta.enemies_random),
  sample_type = "Quasi-experimental",
  model_type  = "Full",
  stringsAsFactors = FALSE
)

# 8) Combine and save
out_models <- rbind(full_all_df, biv_all_df, full_random_df)

print(out_models, row.names = FALSE)

if (!is.null(path_out_csv)) {
  write.csv(out_models, path_out_csv, row.names = FALSE)
}
